package com.ahwei.user.util.easyexcel.listener; /**
 * @author zhangqianwei
 * @date 2022/2/16 20:03
 */

import com.ahwei.user.util.easyexcel.service.ExcelService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 解析监听器，
 * 每解析一行会回调invoke()方法。
 * 整个excel解析结束会执行doAfterAllAnalysed()方法
 * @author ahWei
 */
public class ExcelListener<T> extends AnalysisEventListener {
 
    private final ExcelService<T> excelService;

    /**
     * 解析出来的每一行数据可以存入一个list中
     */
    private List<T> dataList = new ArrayList<>();

    public static Object readData = new ArrayList<>();

    public ExcelListener(ExcelService<T> excelService) {
        this.excelService = excelService;
    }
 
 
    @Override
    public void invoke(Object object, AnalysisContext context) {
        System.out.println("当前行："+context.getCurrentRowNum());
        System.out.println(object);
        //数据存储到list，供批量处理，或后续自己业务逻辑处理。
        dataList.add((T)object);
        //根据自己业务做处理
        //insert(object);
    }
    /*private void insert(Object object) {
        T entity = (T) object;
        excelService.dataImport(entity);
    }*/


    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        readData = excelService.dataImport(dataList);
        // dataList.clear();//解析结束销毁不用的资源
    }
    public List<T> getDataList() {
        return dataList;
    }
    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }
}